<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\video\model;

use think\Model;

/**
 * 视频评论回复点赞 模型
 * @package app\user_center\model
 */
class VideoPlayCommentReplaySupport extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_video_play_comment_reply_support';

    public static function init()
    {
        self::event('after_insert', function ($model) {
            //视频表 点赞数增加
            model('VideoPlayCommentReplay') -> incSupportTimes($model->reply_id);
        });
        self::event('after_delete', function ($model) {
            //点赞数减少
            model('VideoPlayCommentReplay') -> decSupportTimes($model->reply_id);
        });
    }

    /**
     * 添加数据
     * @param int $uid 用户id
     * @param int $reply_id 回复id
     * @author shiqiang <976162041@qq.com>
     * @return bool
     */
    public function addData($uid, $reply_id){
        $this -> uid            = $uid;
        $this -> reply_id       = $reply_id;
        $res = $this -> isUpdate(false) -> save();
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 删除数据
     * @param int $uid 用户id
     * @param int $reply_id 回复id
     * @author shiqiang <976162041@qq.com>
     * @return bool
     */
    public function deleteData($uid, $reply_id){
        $map        = [];
        $map['uid']     = $uid;
        $map['reply_id'] = $reply_id;
        $res = self::destroy(function($query) use ($map){
            $query->where($map);
        });
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 用户是否对某个视频点赞
     * @param int $uid 用户id
     * @param int $page 页码（默认1）
     * @author shiqiang <976162041@qq.com>
     * @return bool
     */
    public function isSupport($uid, $reply_id){
        $map = [];
        $map['uid']     = $uid;
        $map['reply_id'] = $reply_id;
        $info = $this -> where($map) -> find();
        if(!empty($info)){
            return array_out(200, 'success', $info);
        }else{
            return array_out(-1001, 'error');
        }
    }
}
